package com.cyh.pangu.offer;

import com.cyh.pangu.offer.common.ListNode;
import com.cyh.pangu.offer.common.ListUtils;

public class Offer25 {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode newList = new ListNode();
        ListNode cur = newList;
        while (l1 != null && l2 != null) {
            if (l1.val >= l2.val) {
                cur.next = l2;
                l2 = l2.next;

            } else {
                cur.next = l1;
                l1 = l1.next;
            }
            cur = cur.next;
        }
        cur.next = (l1 != null ? l1 : l2 );
        return newList.next;
    }

    public static void main(String[] args) {
        ListNode l1 = ListUtils.buildList(new int[]{1, 2, 4});
        ListNode l2 = ListUtils.buildList(new int[]{1, 3, 4});
        Offer25 offer25 = new Offer25();
        ListNode resultNode = offer25.mergeTwoLists(l1, l2);
        ListUtils.printList(resultNode);
    }
}
